﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using OFX.Aggregator.Infrastructure;

namespace OFX.Aggregator.WpfApplication
{
	/// <summary>
	/// Interaction logic for AccountSetupPage.xaml
	/// </summary>
	public partial class AccountSetupPage : Page
	{
		private readonly UserControl accountSetupPanel;
		private readonly IIoCContainerAbstraction container;
		private readonly StatementSource statementSourceToEdit;
		private readonly StatementSourceTemplate template;

		public AccountSetupPage(StatementSource statementSourceToEdit, IIoCContainerAbstraction container)
		{
			this.statementSourceToEdit = statementSourceToEdit;
			this.container = container;

			InitializeComponent();

			Title = statementSourceToEdit.DisplayName;
			this.textBoxAccountName.Text = statementSourceToEdit.DisplayName;

			this.accountSetupPanel = (!string.IsNullOrEmpty(statementSourceToEdit.Template.AccountSetupPanelId)
			                          	? container.Resolve<UserControl>(statementSourceToEdit.Template.AccountSetupPanelId)
			                          	: null);

			if (this.accountSetupPanel != null)
			{
				var parametersProvider = this.accountSetupPanel as IStatementSourceParametersProvider;

				if (parametersProvider != null)
				{
					parametersProvider.SetParameters(statementSourceToEdit.Parameters);
				}

				this.topLevelGrid.Children.Add(this.accountSetupPanel);
			}
		}

		public AccountSetupPage(StatementSourceTemplate template, IIoCContainerAbstraction container)
		{
			this.template = template;
			this.container = container;

			InitializeComponent();

			Title = template.DisplayName;
			this.textBoxAccountName.Text = template.DisplayName;

			this.accountSetupPanel = (!string.IsNullOrEmpty(template.AccountSetupPanelId)
			                          	? container.Resolve<UserControl>(template.AccountSetupPanelId)
			                          	: null);

			if (this.accountSetupPanel != null)
			{
				this.topLevelGrid.Children.Add(this.accountSetupPanel);
			}
		}

		private void buttonOK_Click(object sender, RoutedEventArgs e)
		{
			var parametersProvider = this.accountSetupPanel as IStatementSourceParametersProvider;
			IStatementSourceRepository statementSourceRepository = null;
			StatementSource target = this.statementSourceToEdit;

			if (target == null)
			{
				statementSourceRepository = this.container.Resolve<IStatementSourceRepository>();
				var newStatementSource =
					this.container.Resolve<StatementSource>(new Dictionary<string, object> {{"templateId", this.template.Id}});

				target = newStatementSource;
			}

			target.DisplayName = this.textBoxAccountName.Text;
			if (parametersProvider != null)
			{
				target.Parameters = parametersProvider.GetParameters();
			}

			if (statementSourceRepository != null)
			{
				statementSourceRepository.StatementSources.Add(target);
			}

			var page = this.container.Resolve<Page>("StatementSourceListPage");
			NavigationService.Navigate(page);
		}
	}
}